package monkey.myinterface;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Search extends Activity {
	/** Called when the activity is first created. */
	private RadioGroup mSearchGroup;
	private RadioButton mSearchName;
	private RadioButton mSearchTag;
	private EditText mSearchEdit;
	private Button mSearchOK;
	private Button mSearchCancel;
	private int mSearchCategory = 0;
	final String[] mSearchKind = { "user", "time" };
	Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		mContext = this;
		mSearchGroup = (RadioGroup) findViewById(R.id.search_radio);
		mSearchName = (RadioButton) findViewById(R.id.name_radiobutton);
		mSearchTag = (RadioButton) findViewById(R.id.tag_radiobutton);
		mSearchEdit = (EditText) findViewById(R.id.search_edit);
		mSearchOK = (Button) findViewById(R.id.search_button);
		mSearchCancel = (Button) findViewById(R.id.searchcancel_button);
		mSearchName.setChecked(true);
		mSearchGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (mSearchName.getId() == checkedId) {
							mSearchCategory = 0;
						} else if (mSearchTag.getId() == checkedId) {
							mSearchCategory = 1;
						}
					}
				});

		mSearchOK.setOnClickListener(new SearchOKListener());
		mSearchCancel.setOnClickListener(new SearchCancelListener());
	}

	class SearchOKListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent mSearchResultIntent = new Intent();
			mSearchResultIntent.setClass(Search.this, SearchResult.class);
			mSearchResultIntent.putExtra("kind", mSearchCategory);
			switch (mSearchCategory) {
			case 0:
				mSearchResultIntent.putExtra("name", mSearchEdit.getText()
						.toString());
				break;
			case 1:
				mSearchResultIntent.putExtra("tag", mSearchEdit.getText()
						.toString());
				break;

			}

			mContext.startActivity(mSearchResultIntent);

		}
	}

	class SearchCancelListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			finish();
		}
	}
}
